import { message } from 'antd';
import { dictionaryList } from '@/services/dictionary';
import { getCompanyOfferDetail, getOfferDetail } from '@/services/myRecruit/offerDetail';
import { getRadioOptionData } from '@/pages/Flow/Mine/ApplyPage/utils';

const initialState = {
    detail: {},
    sex: []
};

export default {
    namespace: 'offerDetail',
    state: initialState,
    effects: {
        *fetchDetail({ payload, callback }, { call, put }) {
            const { offerCode, type } = payload;
            const result = yield call(type === 'mine' ? getOfferDetail : getCompanyOfferDetail, offerCode);

            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        detail: result.data
                    }
                });
                if (callback) callback(result.data);
            } else {
                message.error(result.msg);
            }
        },
        *fetchSex(_, { call, put }) {
            const response = yield call(dictionaryList, {
                code: 'sexType'
            });
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        sex: getRadioOptionData(response.data, {
                            key: 'dictValue',
                            label: 'dictName'
                        })
                    }
                });
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        reset() {
            return initialState;
        }
    }
};
